﻿using System;

namespace Theoreme.Teaching.AOP.Sample01
{
    public class Test_Security
    {
        public static void Run()
        {
            Organization contoso    = new Organization();
            Organization microsoft  = new Organization();

            User britt              = new User( "britt" );

            contoso.UserRoles.AddRole( britt, Roles.SalesAssistant );

            UserContext.AuthenticatedUser = britt;

            Order contosoOrder = null;
            CatchException("Creating Contoso order", () => contosoOrder = new Order(contoso));
            
            Order microsoftOrder = null;            
            CatchException("Creating Microsoft order", () => microsoftOrder = new Order(microsoft));


            CatchException( "Setting discount to Contoso order", () => contosoOrder.Discount = 100 );
            
        }

        private static void CatchException(string name, Action action)
        {
            try
            {
                action();
                Console.WriteLine(name + ": successful");
            }
            catch ( Exception e )
            {
                Console.WriteLine(name + ": " + e.Message);
                
            }
        }
    }
}